/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[ArithmeticTag.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package jsp.tag;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;

/**
 * 一个简单的tag[实现了SimpleTag接口], 实现加减乘除算数运算
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class ArithmeticTag implements SimpleTag {

	private PageContext pageContext;
	private String param1;
	private String param2;
	private String operator;

	public void setParam1(String param1) {
		this.param1 = param1;
	}

	public void setParam2(String param2) {
		this.param2 = param2;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	/**
	 * 定义逻辑的方法
	 */
	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = pageContext.getOut();
		switch (operator) {
		case "+":
			out.print(Integer.parseInt(param1) + Integer.parseInt(param2));
			break;
		case "-":
			out.print(Integer.parseInt(param1) - Integer.parseInt(param2));
			break;
		case "*":
			out.print(Integer.parseInt(param1) * Integer.parseInt(param2));
			break;
		case "/":
			out.print(Integer.parseInt(param1) / Integer.parseInt(param2));
		default:
			break;
		}
	}

	@Override
	public JspTag getParent() {
		return null;
	}

	@Override
	public void setJspBody(JspFragment jspBody) {
	}

	@Override
	public void setJspContext(JspContext jspContext) {
		pageContext = (PageContext) jspContext;
	}

	@Override
	public void setParent(JspTag parent) {
	}
}
